package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.CategoryAdapter;
import com.luck.easyfoodbatch.db.ClassDB;
import com.luck.easyfoodbatch.mvp.home.ClassListsModel;
import com.luck.easyfoodbatch.mvp.home.ClassListsPresenter;
import com.luck.easyfoodbatch.mvp.home.ClassListsView;
import com.luck.easyfoodbatch.mvp.model.ClassModel;
import com.luck.easyfoodbatch.ui.ShopListActivity;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/22
 */
public class CategoryFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, SwipeRefreshLayout.OnRefreshListener, ClassListsView {

    @BindView(R.id.rab1)
    RadioButton rab1;
    @BindView(R.id.rab2)
    RadioButton rab2;
    @BindView(R.id.rabs)
    RadioGroup rabs;
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private View rootView;
    private CategoryAdapter adapter;
    private List<ClassModel.DataBean.ImportBean> list = new ArrayList<>();
    private ClassListsModel listsModel;
    private ClassModel.DataBean data;
    private int isRefresh = 0;
    private ClassDB classDB;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_category, container, false);
            ButterKnife.bind(this, rootView);
            initViews();
        }

        return rootView;
    }

    /**
     * 初始化
     */
    private void initViews() {

        classDB = new ClassDB(getActivity());
        listsModel = new ClassListsPresenter(this, mContext);
        listsModel.getClassList(0,isRefresh, "");
        rabs.setOnCheckedChangeListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(manager);
        adapter = new CategoryAdapter(mContext);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                Bundle bundle = new Bundle();
                ClassModel.DataBean.ImportBean bean = list.get(position);
                bundle.putString("cid", bean.getCid());
                startAct(ShopListActivity.class, bundle);
            }
        });
        adapter.addAll(list);
        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rab1:
                if (data != null) {
                    list = data.getImportX();
                } else {
                    list = new ArrayList<>();
                }
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
                break;
            case R.id.rab2:
                if (data != null) {
                    list = data.getDomestic();
                } else {
                    list = new ArrayList<>();
                }
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void onRefresh() {
        isRefresh = 1;
        listsModel.getClassList(0,isRefresh, "");
    }

    @Override
    public void getHomeClassSuccess(ClassModel model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                data = model.getData();
                list = data.getImportX();
                if (list == null) {
                    list = new ArrayList<>();
                }
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
                if (list != null && list.size() > 0) {
                    classDB.saveClassList(list);
                }
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {

    }
}
